#################################################
### Replication Code PCA ########################
#################################################

# packages
library(psych) # PCA
library(tidyverse)  # tidyverse

# set-up
rm(list=ls())
setwd("C:/Users/timba/OneDrive - Universität Bayreuth (1)/Uni/Research General/Inequality and Protests/Empirics")

# load data
load("master/pca_replication_data.rdata")

# select 2019 data
df_2019 <- select(subset(alldata, year == 2019), gini_s, poverty_rate_s, unemployment_s, bad_health_rate_s, community_health_s, char_organizations_s, network_s, church_adherents_s, bachelorshare_s, median_income_s, share_otherincomes_s, share_white_collar_s, age_group_s, turnout_presidential_s, election_clos_states_poppoll_s, election_clos_counties_poppoll_s, pol_fractionalization_ELF_s) %>% 
  na.omit()

# correlation matrix
corr <- cor(df_2019)

# perform PCA using promax rotation
pca <- principal(corr, nfactors=4, rotate = "promax", score =TRUE)
pca$loadings
unclass(round(pca$loadings,2))

# weights for PCs
W <- unclass(pca$weights)

# calculate PCs with weights from 2019
df <- select(alldata, gini_s, poverty_rate_s, unemployment_s, bad_health_rate_s, community_health_s, char_organizations_s, network_s, church_adherents_s, bachelorshare_s, median_income_s, share_otherincomes_s, share_white_collar_s, age_group_s, turnout_presidential_s, election_clos_states_poppoll_s, election_clos_counties_poppoll_s, pol_fractionalization_ELF_s)
pc <- t(t(W) %*% t(df))
pc <- as_tibble(pc)

# binding
alldata <- cbind(alldata, pc)

# renaming
alldata <- rename(alldata, "index_grievance" = RC1, "index_modernization" = RC2, "index_resource_mob" = RC3, "index_political_opp" = RC4)

# standardize indicators
alldata$index_grievance_s <- as.numeric(scale(alldata$index_grievance))
alldata$index_resource_mob_s <- as.numeric(scale(alldata$index_resource_mob))
alldata$index_modernization_s <- as.numeric(scale(alldata$index_modernization))
alldata$index_political_opp_s <- as.numeric(scale(alldata$index_political_opp))

# ordering 
alldata <- relocate(alldata, GEOID, county, year, states, index_grievance_s, index_resource_mob_s, index_modernization_s, index_political_opp_s, gini_s, poverty_rate_s, unemployment_s, bad_health_rate_s, community_health_s, char_organizations_s, network_s, church_adherents_s, bachelorshare_s, median_income_s, share_otherincomes_s, share_white_collar_s, age_group_s, turnout_presidential_s, election_clos_states_poppoll_s, election_clos_counties_poppoll_s, pol_fractionalization_ELF_s)

